using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Text;

namespace Gko.Utils
{
	internal static class NativeMethods
	{
		internal static uint FILE_MAP_ALL_ACCESS = 0xF001F;

		internal static uint PAGE_READWRITE = 0x04;

		private const string Kernel32 = "kernel32.dll";
		private const string PsApi = "Psapi.dll";

		[DllImport(Kernel32, CharSet = CharSet.Auto, BestFitMapping = false, SetLastError = true)]
		internal static extern SafeLibraryHandle LoadLibrary(string fileName);

		[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
		[DllImport(Kernel32, SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool FreeLibrary(IntPtr hModule);

		[DllImport(Kernel32)]
		internal static extern IntPtr GetProcAddress(SafeLibraryHandle hModule, String procname);

		[DllImport(Kernel32, SetLastError = true)]
		internal static extern int SetSystemTime(ref NativeSystemTime time);

		/// Return Type: HANDLE->void*
		///hFile: HANDLE->void*
		///lpFileMappingAttributes: LPSECURITY_ATTRIBUTES->_SECURITY_ATTRIBUTES*
		///flProtect: DWORD->unsigned int
		///dwMaximumSizeHigh: DWORD->unsigned int
		///dwMaximumSizeLow: DWORD->unsigned int
		///lpName: LPCWSTR->WCHAR*
		[DllImport(Kernel32, EntryPoint = "CreateFileMappingW")]
		internal static extern IntPtr CreateFileMappingW([In] IntPtr hFile, [In] IntPtr lpFileMappingAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, [In] [MarshalAs(UnmanagedType.LPWStr)] string lpName);

		/// Return Type: HANDLE->void*
		///dwDesiredAccess: DWORD->unsigned int
		///bInheritHandle: BOOL->int
		///lpName: LPCWSTR->WCHAR*
		[DllImport(Kernel32, EntryPoint = "OpenFileMappingW")]
		internal static extern IntPtr OpenFileMappingW(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, [In] [MarshalAs(UnmanagedType.LPWStr)] string lpName);

		/// Return Type: LPVOID->void*
		///hFileMappingObject: HANDLE->void*
		///dwDesiredAccess: DWORD->unsigned int
		///dwFileOffsetHigh: DWORD->unsigned int
		///dwFileOffsetLow: DWORD->unsigned int
		///dwNumberOfBytesToMap: SIZE_T->ULONG_PTR->unsigned int
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", MessageId = "4"), DllImport(Kernel32, EntryPoint = "MapViewOfFile")]
		internal static extern IntPtr MapViewOfFile([In] IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);

		/// Return Type: LPVOID->void*
		///hFileMappingObject: HANDLE->void*
		///dwDesiredAccess: DWORD->unsigned int
		///dwFileOffsetHigh: DWORD->unsigned int
		///dwFileOffsetLow: DWORD->unsigned int
		///dwNumberOfBytesToMap: SIZE_T->ULONG_PTR->unsigned int
		///lpBaseAddress: LPVOID->void*
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", MessageId = "4"), DllImport(Kernel32, EntryPoint = "MapViewOfFileEx")]
		internal static extern IntPtr MapViewOfFileEx([In] IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap, [In] IntPtr lpBaseAddress);

		/// Return Type: BOOL->int
		///lpBaseAddress: LPCVOID->void*
		///dwNumberOfBytesToFlush: SIZE_T->ULONG_PTR->unsigned int
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", MessageId = "1"), DllImport(Kernel32, EntryPoint = "FlushViewOfFile")]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool FlushViewOfFile([In] IntPtr lpBaseAddress, uint dwNumberOfBytesToFlush);

		/// Return Type: BOOL->int
		///lpBaseAddress: LPCVOID->void*
		[DllImport(Kernel32, EntryPoint = "UnmapViewOfFile")]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool UnmapViewOfFile([In] IntPtr lpBaseAddress);

		/// Return Type: DWORD->unsigned int
		///hProcess: HANDLE->void*
		///lpv: LPVOID->void*
		///lpFilename: LPTSTR->LPWSTR->WCHAR*
		///nSize: DWORD->unsigned int
		[DllImport(PsApi, EntryPoint = "GetMappedFileNameW")]
		internal static extern uint GetMappedFileNameW([In] IntPtr hProcess, [In] IntPtr lpv, [Out] [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpFilename, uint nSize);

		/// Return Type: HANDLE->void*
		[DllImport(Kernel32, EntryPoint = "GetCurrentProcess")]
		internal static extern IntPtr GetCurrentProcess();

		/// Return Type: BOOL->int
		///hObject: HANDLE->void*
		[DllImport(Kernel32, EntryPoint = "CloseHandle")]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool CloseHandle([In] IntPtr hObject);

		#region Nested type: SECURITY_ATTRIBUTES

		[StructLayout(LayoutKind.Sequential)]
		internal struct SECURITY_ATTRIBUTES
		{
			/// DWORD->unsigned int
			public uint nLength;

			/// LPVOID->void*
			public IntPtr lpSecurityDescriptor;

			/// BOOL->int
			[MarshalAs(UnmanagedType.Bool)]
			public bool bInheritHandle;
		}

		#endregion
	}
}